/*
 * Copyright 2017-8-30 13:56:25 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.core.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scp.admin.core.UserManager;
import com.lap.scp.admin.dal.UserDAO;
import com.lap.scp.admin.domain.UserDO;
import com.lap.scp.admin.domain.UserRoleDO;
import com.lap.scp.admin.query.UserQueryDO;
import com.lap.scp.admin.support.MybatisBatch;

/**
 * <pre>
 * // TODO 注释
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:25
 * @since 1.0
 */
@Service("userManager")
public class DefaultUserManager implements UserManager {

	@Resource
	private UserDAO userDAO;

	@Resource
	private MybatisBatch mybatisBatch;

	@Override
	public int deleteById(int userId) throws ManagerException {
		return userDAO.deleteById(userId);
	}

	@Override
	public void insertUser(UserDO userDO) throws ManagerException {
		userDAO.insertUser(userDO);
	}

	@Override
	public UserDO queryById(int userId) throws ManagerException {
		return userDAO.queryById(userId);
	}

	@Override
	public List<UserDO> queryPage(Page page, UserQueryDO queryDO) throws ManagerException {
		int count = userDAO.countPage(queryDO);
		if (count <= 0) {
			return Collections.emptyList();
		} else {
			page.setTotalItem(count);
			return userDAO.queryPage(page, queryDO);
		}
	}

	@Override
	public int updateUser(UserDO userDO) throws ManagerException {
		return userDAO.updateUser(userDO);
	}

	@Override
	public int updateStatus(int userId, int status, String updatedBy) throws ManagerException {
		return userDAO.updateStatus(userId, status, updatedBy);
	}

	@Override
	public int updatePasswd(int userId, String newPd, String lastUpdateBy) throws ManagerException {
		return userDAO.updatePasswd(userId, newPd, lastUpdateBy);
	}

	@Override
	public int bindRole(int userId, List<UserRoleDO> roleIdList) throws ManagerException {
		int flag = userDAO.deleteRoleById(userId);
		mybatisBatch.insertBatch("com.lap.scp.admin.dal.UserDAO.insertUserRole", roleIdList, true);
		return flag;
	}

	@Override
	public List<Integer> queryRoleIdById(int userId) throws ManagerException {
		return userDAO.queryRoleIdById(userId);
	}

	@Override
	public UserDO queryByUsername(String username) throws ManagerException {
		return userDAO.queryByUsername(username);
	}

}
